package com.eco.mam.work.request;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.eco.mam.work.ConfigParameters;
import com.eco.mam.work.request.dto.WorkRequest;
import com.eco.mam.work.request.resource.WorkRequestConfig;

public class WorkRequestUtils {
    void writeToConsole(StringWriter stringWriter) {

        System.out.println(stringWriter);

    }

    public void writeToFile(StringWriter stringWriter) {

        FileWriter fileWriter;
        try {
            String strXMLOutPutFileName = WorkRequestConfig
                    .getFileName(ConfigParameters.XML_OUTPUT_FILE);
            fileWriter = new FileWriter(new File(strXMLOutPutFileName));
            fileWriter.write(stringWriter.toString());
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void writeToFile(StringBuffer missingData) {
        FileWriter fileWriter;
        try {
            BufferedWriter writer = getWriter(WorkRequestConfig
                    .getFileName(ConfigParameters.MISSING_DATA_OUTPUT_FILE));
            String strMissingData = missingData.toString();
            String strMissingDatas[] = strMissingData.split("\n");
            for (int i = 0; i < strMissingDatas.length; i++) {
                writer.write((strMissingDatas[i]));
                writer.newLine();
                writer.newLine();

            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void writeToFile(ArrayList<String> invalidRequests) {

        FileWriter fileWriter;
        try {
            BufferedWriter writer = getWriter(WorkRequestConfig
                    .getFileName(ConfigParameters.INVALID_CSV_REQUESTS_FILE));

            for (int i = 0; i < invalidRequests.size(); i++) {
                writer.write((invalidRequests.get(i)));
                writer.newLine();
                writer.newLine();

            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * @param strXMLOutPutFileName
     * @return
     * @throws IOException
     */
    private BufferedWriter getWriter(String strXMLOutPutFileName)
            throws IOException {
        FileWriter fileWriter;
        fileWriter = new FileWriter(new File(strXMLOutPutFileName));
        BufferedWriter writer = new BufferedWriter(fileWriter);
        return writer;
    }

    public void writeToFileAsXMLUsingJABX(ArrayList<WorkRequest> workRequestList) {
        Writer writer = null;
        try {
            JAXBContext context = JAXBContext.newInstance(WorkRequest.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            writer = new FileWriter("jabx.xml");
            for (int i = 0; i < workRequestList.size(); i++) {

                marshaller.marshal(workRequestList.get(i), writer);
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
            }
        }

    }

}
